﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yc.Dal;
using yc.Site.Models;

namespace yc.Site.Controllers
{
	public class AccountController : Controller
	{
		public ActionResult ChangePassword()
		{
			return View();
		}

		[HttpPost, ActionName("ChangePassword")]
		public ActionResult PostChangePassword(PasswordEntity model)
		{
			var newmodel = new PasswordEntity();
			newmodel.OldPwd = MD5.Encrypt(model.OldPwd);
			newmodel.NewPwd = MD5.Encrypt(model.NewPwd);

			if (!newmodel.OldPwd.Equals(Common.User.Password)) ModelState.AddModelError("OldPwd", "你输入的旧密码不正确！");

			if (ModelState.IsValid)
			{
				using (var db = new DBContext())
				{
					db.Configuration.AutoDetectChangesEnabled = true;

					var u = db.SysUser.Find(Common.User.ID);
					u.Password = newmodel.NewPwd;
					db.SaveChanges();

					Common.User.Password = newmodel.NewPwd;
				}
			}

			return View(model);
		}
	}
}
